<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />

<meta name="author" content="Aubrey Dugger" />

<meta name="date" content="2016-05-03" />

<title>Download and process MODIS LAI images</title>



<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
  margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; }
code > span.dt { color: #902000; }
code > span.dv { color: #40a070; }
code > span.bn { color: #40a070; }
code > span.fl { color: #40a070; }
code > span.ch { color: #4070a0; }
code > span.st { color: #4070a0; }
code > span.co { color: #60a0b0; font-style: italic; }
code > span.ot { color: #007020; }
code > span.al { color: #ff0000; font-weight: bold; }
code > span.fu { color: #06287e; }
code > span.er { color: #ff0000; font-weight: bold; }
</style>
<style type="text/css">
  pre:not([class]) {
    background-color: white;
  }
</style>


<link href="data:text/css,body%20%7B%0A%20%20background%2Dcolor%3A%20%23fff%3B%0A%20%20margin%3A%201em%20auto%3B%0A%20%20max%2Dwidth%3A%20700px%3B%0A%20%20overflow%3A%20visible%3B%0A%20%20padding%2Dleft%3A%202em%3B%0A%20%20padding%2Dright%3A%202em%3B%0A%20%20font%2Dfamily%3A%20%22Open%20Sans%22%2C%20%22Helvetica%20Neue%22%2C%20Helvetica%2C%20Arial%2C%20sans%2Dserif%3B%0A%20%20font%2Dsize%3A%2014px%3B%0A%20%20line%2Dheight%3A%201%2E35%3B%0A%7D%0A%0A%23header%20%7B%0A%20%20text%2Dalign%3A%20center%3B%0A%7D%0A%0A%23TOC%20%7B%0A%20%20clear%3A%20both%3B%0A%20%20margin%3A%200%200%2010px%2010px%3B%0A%20%20padding%3A%204px%3B%0A%20%20width%3A%20400px%3B%0A%20%20border%3A%201px%20solid%20%23CCCCCC%3B%0A%20%20border%2Dradius%3A%205px%3B%0A%0A%20%20background%2Dcolor%3A%20%23f6f6f6%3B%0A%20%20font%2Dsize%3A%2013px%3B%0A%20%20line%2Dheight%3A%201%2E3%3B%0A%7D%0A%20%20%23TOC%20%2Etoctitle%20%7B%0A%20%20%20%20font%2Dweight%3A%20bold%3B%0A%20%20%20%20font%2Dsize%3A%2015px%3B%0A%20%20%20%20margin%2Dleft%3A%205px%3B%0A%20%20%7D%0A%0A%20%20%23TOC%20ul%20%7B%0A%20%20%20%20padding%2Dleft%3A%2040px%3B%0A%20%20%20%20margin%2Dleft%3A%20%2D1%2E5em%3B%0A%20%20%20%20margin%2Dtop%3A%205px%3B%0A%20%20%20%20margin%2Dbottom%3A%205px%3B%0A%20%20%7D%0A%20%20%23TOC%20ul%20ul%20%7B%0A%20%20%20%20margin%2Dleft%3A%20%2D2em%3B%0A%20%20%7D%0A%20%20%23TOC%20li%20%7B%0A%20%20%20%20line%2Dheight%3A%2016px%3B%0A%20%20%7D%0A%0Atable%20%7B%0A%20%20margin%3A%201em%20auto%3B%0A%20%20border%2Dwidth%3A%201px%3B%0A%20%20border%2Dcolor%3A%20%23DDDDDD%3B%0A%20%20border%2Dstyle%3A%20outset%3B%0A%20%20border%2Dcollapse%3A%20collapse%3B%0A%7D%0Atable%20th%20%7B%0A%20%20border%2Dwidth%3A%202px%3B%0A%20%20padding%3A%205px%3B%0A%20%20border%2Dstyle%3A%20inset%3B%0A%7D%0Atable%20td%20%7B%0A%20%20border%2Dwidth%3A%201px%3B%0A%20%20border%2Dstyle%3A%20inset%3B%0A%20%20line%2Dheight%3A%2018px%3B%0A%20%20padding%3A%205px%205px%3B%0A%7D%0Atable%2C%20table%20th%2C%20table%20td%20%7B%0A%20%20border%2Dleft%2Dstyle%3A%20none%3B%0A%20%20border%2Dright%2Dstyle%3A%20none%3B%0A%7D%0Atable%20thead%2C%20table%20tr%2Eeven%20%7B%0A%20%20background%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0A%0Ap%20%7B%0A%20%20margin%3A%200%2E5em%200%3B%0A%7D%0A%0Ablockquote%20%7B%0A%20%20background%2Dcolor%3A%20%23f6f6f6%3B%0A%20%20padding%3A%200%2E25em%200%2E75em%3B%0A%7D%0A%0Ahr%20%7B%0A%20%20border%2Dstyle%3A%20solid%3B%0A%20%20border%3A%20none%3B%0A%20%20border%2Dtop%3A%201px%20solid%20%23777%3B%0A%20%20margin%3A%2028px%200%3B%0A%7D%0A%0Adl%20%7B%0A%20%20margin%2Dleft%3A%200%3B%0A%7D%0A%20%20dl%20dd%20%7B%0A%20%20%20%20margin%2Dbottom%3A%2013px%3B%0A%20%20%20%20margin%2Dleft%3A%2013px%3B%0A%20%20%7D%0A%20%20dl%20dt%20%7B%0A%20%20%20%20font%2Dweight%3A%20bold%3B%0A%20%20%7D%0A%0Aul%20%7B%0A%20%20margin%2Dtop%3A%200%3B%0A%7D%0A%20%20ul%20li%20%7B%0A%20%20%20%20list%2Dstyle%3A%20circle%20outside%3B%0A%20%20%7D%0A%20%20ul%20ul%20%7B%0A%20%20%20%20margin%2Dbottom%3A%200%3B%0A%20%20%7D%0A%0Apre%2C%20code%20%7B%0A%20%20background%2Dcolor%3A%20%23f7f7f7%3B%0A%20%20border%2Dradius%3A%203px%3B%0A%20%20color%3A%20%23333%3B%0A%7D%0Apre%20%7B%0A%20%20white%2Dspace%3A%20pre%2Dwrap%3B%20%20%20%20%2F%2A%20Wrap%20long%20lines%20%2A%2F%0A%20%20border%2Dradius%3A%203px%3B%0A%20%20margin%3A%205px%200px%2010px%200px%3B%0A%20%20padding%3A%2010px%3B%0A%7D%0Apre%3Anot%28%5Bclass%5D%29%20%7B%0A%20%20background%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0A%0Acode%20%7B%0A%20%20font%2Dfamily%3A%20Consolas%2C%20Monaco%2C%20%27Courier%20New%27%2C%20monospace%3B%0A%20%20font%2Dsize%3A%2085%25%3B%0A%7D%0Ap%20%3E%20code%2C%20li%20%3E%20code%20%7B%0A%20%20padding%3A%202px%200px%3B%0A%7D%0A%0Adiv%2Efigure%20%7B%0A%20%20text%2Dalign%3A%20center%3B%0A%7D%0Aimg%20%7B%0A%20%20background%2Dcolor%3A%20%23FFFFFF%3B%0A%20%20padding%3A%202px%3B%0A%20%20border%3A%201px%20solid%20%23DDDDDD%3B%0A%20%20border%2Dradius%3A%203px%3B%0A%20%20border%3A%201px%20solid%20%23CCCCCC%3B%0A%20%20margin%3A%200%205px%3B%0A%7D%0A%0Ah1%20%7B%0A%20%20margin%2Dtop%3A%200%3B%0A%20%20font%2Dsize%3A%2035px%3B%0A%20%20line%2Dheight%3A%2040px%3B%0A%7D%0A%0Ah2%20%7B%0A%20%20border%2Dbottom%3A%204px%20solid%20%23f7f7f7%3B%0A%20%20padding%2Dtop%3A%2010px%3B%0A%20%20padding%2Dbottom%3A%202px%3B%0A%20%20font%2Dsize%3A%20145%25%3B%0A%7D%0A%0Ah3%20%7B%0A%20%20border%2Dbottom%3A%202px%20solid%20%23f7f7f7%3B%0A%20%20padding%2Dtop%3A%2010px%3B%0A%20%20font%2Dsize%3A%20120%25%3B%0A%7D%0A%0Ah4%20%7B%0A%20%20border%2Dbottom%3A%201px%20solid%20%23f7f7f7%3B%0A%20%20margin%2Dleft%3A%208px%3B%0A%20%20font%2Dsize%3A%20105%25%3B%0A%7D%0A%0Ah5%2C%20h6%20%7B%0A%20%20border%2Dbottom%3A%201px%20solid%20%23ccc%3B%0A%20%20font%2Dsize%3A%20105%25%3B%0A%7D%0A%0Aa%20%7B%0A%20%20color%3A%20%230033dd%3B%0A%20%20text%2Ddecoration%3A%20none%3B%0A%7D%0A%20%20a%3Ahover%20%7B%0A%20%20%20%20color%3A%20%236666ff%3B%20%7D%0A%20%20a%3Avisited%20%7B%0A%20%20%20%20color%3A%20%23800080%3B%20%7D%0A%20%20a%3Avisited%3Ahover%20%7B%0A%20%20%20%20color%3A%20%23BB00BB%3B%20%7D%0A%20%20a%5Bhref%5E%3D%22http%3A%22%5D%20%7B%0A%20%20%20%20text%2Ddecoration%3A%20underline%3B%20%7D%0A%20%20a%5Bhref%5E%3D%22https%3A%22%5D%20%7B%0A%20%20%20%20text%2Ddecoration%3A%20underline%3B%20%7D%0A%0A%2F%2A%20Class%20described%20in%20https%3A%2F%2Fbenjeffrey%2Ecom%2Fposts%2Fpandoc%2Dsyntax%2Dhighlighting%2Dcss%0A%20%20%20Colours%20from%20https%3A%2F%2Fgist%2Egithub%2Ecom%2Frobsimmons%2F1172277%20%2A%2F%0A%0Acode%20%3E%20span%2Ekw%20%7B%20color%3A%20%23555%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%2F%2A%20Keyword%20%2A%2F%0Acode%20%3E%20span%2Edt%20%7B%20color%3A%20%23902000%3B%20%7D%20%2F%2A%20DataType%20%2A%2F%0Acode%20%3E%20span%2Edv%20%7B%20color%3A%20%2340a070%3B%20%7D%20%2F%2A%20DecVal%20%28decimal%20values%29%20%2A%2F%0Acode%20%3E%20span%2Ebn%20%7B%20color%3A%20%23d14%3B%20%7D%20%2F%2A%20BaseN%20%2A%2F%0Acode%20%3E%20span%2Efl%20%7B%20color%3A%20%23d14%3B%20%7D%20%2F%2A%20Float%20%2A%2F%0Acode%20%3E%20span%2Ech%20%7B%20color%3A%20%23d14%3B%20%7D%20%2F%2A%20Char%20%2A%2F%0Acode%20%3E%20span%2Est%20%7B%20color%3A%20%23d14%3B%20%7D%20%2F%2A%20String%20%2A%2F%0Acode%20%3E%20span%2Eco%20%7B%20color%3A%20%23888888%3B%20font%2Dstyle%3A%20italic%3B%20%7D%20%2F%2A%20Comment%20%2A%2F%0Acode%20%3E%20span%2Eot%20%7B%20color%3A%20%23007020%3B%20%7D%20%2F%2A%20OtherToken%20%2A%2F%0Acode%20%3E%20span%2Eal%20%7B%20color%3A%20%23ff0000%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%2F%2A%20AlertToken%20%2A%2F%0Acode%20%3E%20span%2Efu%20%7B%20color%3A%20%23900%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%2F%2A%20Function%20calls%20%2A%2F%20%0Acode%20%3E%20span%2Eer%20%7B%20color%3A%20%23a61717%3B%20background%2Dcolor%3A%20%23e3d2d2%3B%20%7D%20%2F%2A%20ErrorTok%20%2A%2F%0A%0A" rel="stylesheet" type="text/css" />

</head>

<body>



<div id="header">
<h1 class="title">Download and process MODIS LAI images</h1>
<h4 class="author"><em>Aubrey Dugger</em></h4>
<h4 class="date"><em>2016-05-03</em></h4>
</div>


<div id="background" class="section level1">
<h1>Background</h1>
<p>We are using WRF-Hydro to predict streamflow for Fourmile Creek at the Orodell USGS gage for the 2013 snowmelt period. We want to obtain MODIS LAI images for our region of interest and insert the processed images into our forcing time series so we can use dynamic LAI in future WRF-Hydro model runs.</p>
<p>Load the rwrfhydro package.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(<span class="st">&quot;rwrfhydro&quot;</span>)</code></pre>
<pre><code>## To check rwrfhydro updates run: CheckForUpdates()</code></pre>
<p>Set a data path to the Fourmile Creek test case.</p>
<pre class="sourceCode r"><code class="sourceCode r">dataPath &lt;-<span class="st"> '~/wrfHydroTestCases/Fourmile_Creek_testcase_v2.0/'</span></code></pre>
</div>
<div id="setup-the-modis-r-tool." class="section level1">
<h1>Setup the MODIS-R tool.</h1>
<p>NOTE: These tools rely heavily on the R-Forge <a href="https://r-forge.r-project.org/projects/modis/">MODIS-R</a> package, so please see their development page and R help pages for more on the package tools and options. Currently, we are making some edits to the MODIS tool to add some capacity specific to WRF-Hydro applications. So please install the rwrfhydro version of MODIS from github (<code>devtools::install_github(&quot;aubreyd/modis4rwrfhydro&quot;)</code>). A local system <a href="http://www.gdal.org/">GDAL</a> install is also required.</p>
<p>To start, we need to load a few libraries and then tell MODIS-R where our working directories are. If this is the first time using MODIS-R, we will also need to tell it where the local GDAL libraries live. Your GDAL pathname may vary; type “which gdalinfo” in a terminal window to find the appropriate path for your system. If gdalinfo is not found, make sure you have GDAL installed on your local system.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(rgeos)</code></pre>
<pre><code>## Warning: package 'rgeos' was built under R version 3.1.3</code></pre>
<pre><code>## rgeos version: 0.3-11, (SVN revision 479)
##  GEOS runtime version: 3.4.2-CAPI-1.8.2 r3921 
##  Linking to sp version: 1.1-0 
##  Polygon checking: TRUE</code></pre>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(rgdal)</code></pre>
<pre><code>## Warning: package 'rgdal' was built under R version 3.1.3</code></pre>
<pre><code>## Loading required package: sp</code></pre>
<pre><code>## Warning: package 'sp' was built under R version 3.1.3</code></pre>
<pre><code>## rgdal: version: 0.9-3, (SVN revision 530)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 1.11.2, released 2015/02/10
##  Path to GDAL shared files: /Users/adugger/Library/R/3.1/library/rgdal/gdal
##  Loaded PROJ.4 runtime: Rel. 4.9.1, 04 March 2015, [PJ_VERSION: 491]
##  Path to PROJ.4 shared files: /Users/adugger/Library/R/3.1/library/rgdal/proj
##  Linking to sp version: 1.1-0</code></pre>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(raster)</code></pre>
<pre><code>## Warning: package 'raster' was built under R version 3.1.3</code></pre>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(MODIS)</code></pre>
<pre><code>## MODIS_manual: https://ivfl-rio.boku.ac.at/owncloud/public.php?service=files&amp;t=660dc830afb091237cc40b3dea2fdf6b</code></pre>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ncdf4)

## if gdalPath returns 1=error, then try these two lines, substiting your shell profile script as appropriate.
<span class="co"># bashPath &lt;- system('. ~/.bash_profile; echo $PATH', intern = TRUE)</span>
<span class="co"># Sys.setenv(PATH=bashPath)</span>
gdalPath &lt;-<span class="st"> </span><span class="kw">sub</span>(<span class="st">&quot;gdalinfo&quot;</span>, <span class="dt">replacement=</span><span class="st">&quot;&quot;</span>, <span class="kw">system</span>(<span class="st">&quot;which gdalinfo&quot;</span>, <span class="dt">intern=</span><span class="ot">TRUE</span>))
<span class="kw">MODISoptions</span>(<span class="dt">localArcPath=</span><span class="st">&quot;~/wrfHydroTestCases/MODIS_ARC&quot;</span>, 
             <span class="dt">outDirPath=</span><span class="st">&quot;~/wrfHydroTestCases/MODIS_ARC/PROCESSED&quot;</span>, <span class="dt">gdalPath=</span>gdalPath,
             <span class="dt">stubbornness=</span><span class="st">&quot;low&quot;</span>)</code></pre>
<pre><code>## All suggested packages are installed</code></pre>
<pre><code>## Setting 'localArcPath' to '/Volumes/d1/adugger/WRF_Hydro/test_cases/MODIS_ARC'
## If you already have downloaded some HDF-files to '/Volumes/d1/adugger/WRF_Hydro/test_cases/MODIS_ARC' you can use '?orgStruc()' to re-arrange your HDF-data!
## 'outDirPath' has been changed from '/Volumes/d1/adugger/WRF_Hydro/test_cases/MODIS_ARC/PROCESSED' to '/Volumes/d1/adugger/WRF_Hydro/test_cases/MODIS_ARC/PROCESSED'</code></pre>
<pre><code>##   'MRT_HOME' not set/found! MRT is NOT enabled! See: 'https://lpdaac.usgs.gov/tools/modis_reprojection_tool'
## 
## STORAGE:
## _______________
## localArcPath : /Volumes/d1/adugger/WRF_Hydro/test_cases/MODIS_ARC 
## outDirPath   : /Volumes/d1/adugger/WRF_Hydro/test_cases/MODIS_ARC/PROCESSED 
## 
## 
## DOWNLOAD:
## _______________
## MODISserverOrder : LPDAAC, LAADS 
## dlmethod         : auto 
## stubbornness     : low 
## 
## 
## PROCESSING:
## _______________
## GDAL           : GDAL 1.11.4, released 2016/01/25 
## MRT            : Not available. Use 'MODIS:::checkTools('MRT')' for more information! 
## pixelSize      : asIn 
## outProj        : asIn 
## resamplingType : NN 
## dataFormat     : GTiff 
## 
## 
## DEPENDENCIES:
## _______________
## </code></pre>
<pre><code>## NULL</code></pre>
</div>
<div id="download-and-process-modis-tiles" class="section level1">
<h1>Download and process MODIS tiles</h1>
<p>Here we download the relevant MODIS tiles and resample to the Fourmile Creek geogrid. The GetMODIS tool calls the MODIS-R runGdal tool to identify the required MODIS tiles to cover the domain, download the appropriate product tiles, mosaic them, and clip/resample (nearest neighbor) to the geogrid extent and resolution. The result will be a directory of processed TIF images in the outDirPath specified in MODISoptions. We specify the geogrid, a start and end date, and the MOD15A2 (FPAR/LAI) MODIS product name.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">GetMODIS</span>(<span class="dt">extent=</span><span class="kw">paste0</span>(dataPath, <span class="st">'/DOMAIN/geo_em_d01.Fourmile1km.nlcd11.nc'</span>), <span class="dt">prodName=</span><span class="st">&quot;MOD15A2&quot;</span>,  
         <span class="dt">outDir=</span><span class="st">&quot;FOURMILE_LAI&quot;</span>, <span class="dt">begin=</span><span class="st">&quot;2013.03.01&quot;</span>, <span class="dt">end=</span><span class="st">&quot;2013.07.31&quot;</span>,
         <span class="dt">exclList=</span><span class="kw">list</span>(<span class="st">&quot;Fpar_1km&quot;</span>=<span class="st">&quot;gt 100&quot;</span>, 
                       <span class="st">&quot;Lai_1km&quot;</span>=<span class="st">&quot;gt 100&quot;</span>, 
                       <span class="st">&quot;FparLai_QC&quot;</span>=<span class="st">&quot;255&quot;</span>, 
                       <span class="st">&quot;FparExtra_QC&quot;</span>=<span class="st">&quot;255&quot;</span>, 
                       <span class="st">&quot;FparStdDev_1km&quot;</span>=<span class="st">&quot;gt 100&quot;</span>, 
                       <span class="st">&quot;LaiStdDev_1km&quot;</span>=<span class="st">&quot;gt 100&quot;</span>), 
         <span class="dt">resampList=</span><span class="kw">list</span>(<span class="st">&quot;Fpar_1km&quot;</span>=<span class="st">&quot;bilinear&quot;</span>, 
                       <span class="st">&quot;Lai_1km&quot;</span>=<span class="st">&quot;bilinear&quot;</span>, 
                       <span class="st">&quot;FparLai_QC&quot;</span>=<span class="st">&quot;mode&quot;</span>, 
                       <span class="st">&quot;FparExtra_QC&quot;</span>=<span class="st">&quot;mode&quot;</span>, 
                       <span class="st">&quot;FparStdDev_1km&quot;</span>=<span class="st">&quot;bilinear&quot;</span>, 
                       <span class="st">&quot;LaiStdDev_1km&quot;</span>=<span class="st">&quot;bilinear&quot;</span>),
         <span class="dt">quiet=</span><span class="ot">TRUE</span>)</code></pre>
<pre><code>## Assuming file is a geogrid in netcdf format.</code></pre>
<pre><code>## ########################
## outProj          =  +proj=lcc +lat_1=30 +lat_2=50 +lat_0=40.0403709411621 +lon_0=-105 +x_0=0 +y_0=0 +a=6370000 +b=6370000 +units=m +no_defs  (Specified by raster*/spatial* object)
## pixelSize        =  1000 1000  (Specified by raster* object)
## resamplingType   =  near 
## Output Directory =  /Volumes/d1/adugger/WRF_Hydro/test_cases/MODIS_ARC/PROCESSED/FOURMILE_LAI 
## ########################</code></pre>
<p>Create a raster stack of the downloaded and processed MODIS LAI images. This tool does some additional processing of the images to get them into a more usable format. Specifically, it can remove “no data” values (in this case, we already excluded those before reasmpling in the step above) and can apply product-specific scale factors (in this case, 0.1). See the <a href="https://lpdaac.usgs.gov/products/modis_products_table">MODIS data page</a> for specifics on valid value ranges and scale factors.</p>
<pre class="sourceCode r"><code class="sourceCode r">lai.b &lt;-<span class="st"> </span><span class="kw">ConvertRS2Stack</span>(<span class="kw">paste0</span>(<span class="kw">options</span>(<span class="st">&quot;MODIS_outDirPath&quot;</span>), <span class="st">'/FOURMILE_LAI'</span>), <span class="st">&quot;*Lai_1km.tif$&quot;</span>, 
                         <span class="dt">begin=</span><span class="st">&quot;2013.03.01&quot;</span>, <span class="dt">end=</span><span class="st">&quot;2013.07.31&quot;</span>, 
                         <span class="dt">valScale=</span><span class="fl">0.1</span>, <span class="dt">valAdd=</span><span class="dv">0</span>)</code></pre>
<pre><code>## Found 19 files!
## Found 19 files!</code></pre>
<p>See the help on ConvertRS2Stack for more details on the processing options.</p>
<pre class="sourceCode r"><code class="sourceCode r">?ConvertRS2Stack</code></pre>
<div style="border:1px solid; border-radius: 25px; padding: 12px 25px;">
<div class="r-help-page">
<table width="100%" summary="page for ConvertRS2Stack">
<tr>
<td>
ConvertRS2Stack
</td>
<td align="right">
R Documentation
</td>
</tr>
</table>
<h2>
Convert a set of MODIS images to a raster stack and, optionally, a NetCDF file.
</h2>
<h3>
Description
</h3>
<p>
<code>ConvertRS2Stack</code> takes a set of pre-processed MODIS TIF files and creates a raster stack and, optionally, a NetCDF file.
</p>
<h3>
Usage
</h3>
<pre class="r">
ConvertRS2Stack(inPath, matchStr, begin = NULL, end = NULL, noData = NULL,
  noDataQual = &quot;exact&quot;, valScale = 1, valAdd = 0, outFile = NULL,
  varName = NULL, varUnit = NULL, varLong = NULL, varNA = (-1e+36),
  pos1 = 10, pos2 = 16, format = &quot;%Y%j&quot;)
</pre>
<h3>
Arguments
</h3>
<table summary="R argblock">
<tr valign="top">
<td>
<code>inPath</code>
</td>
<td>
<p>
The path to the directory that holds the already processed MODIS TIF files.
</p>
</td>
</tr>
<tr valign="top">
<td>
<code>matchStr</code>
</td>
<td>
<p>
The regular expression for filename matching (e.g., “*Lai_1km.tif“).
</p>
</td>
</tr>
<tr valign="top">
<td>
<code>begin</code>
</td>
<td>
<p>
Date string for the start date to include. The date string should follow <a href="http://r-forge.r-project.org/projects/modis/">MODIS</a> package convention (e.g., “2011.06.01”). (DEFAULT=NULL, all files are processed).
</p>
</td>
</tr>
<tr valign="top">
<td>
<code>end</code>
</td>
<td>
<p>
Date string for the end date to include. The date string should follow <a href="http://r-forge.r-project.org/projects/modis/">MODIS</a> package convention (e.g., “2011.06.01”). (DEFAULT=NULL, all files are processed).
</p>
</td>
</tr>
<tr valign="top">
<td>
<code>noData</code>
</td>
<td>
<p>
Value for specifying “no data” per the MODIS product. Should be combined with a qualifier. For example, for the MOD15A2 (LAI) product, valid value range is 0-100. So the noData value should be set to 100 and the noDataQual to “max”. (DEFAULT=NULL, no additional “no data” screening is applied)
</p>
</td>
</tr>
<tr valign="top">
<td>
<code>noDataQual</code>
</td>
<td>
<p>
Qualifier for specifying “no data” per the MODIS product. Should be combined with a nodata value. For example, for the MOD15A2 LAI product, valid value range is 0-100. So the noData value should be set to 100 and the noDataQual to “max”. Options are: “exact” (value == noData are converted to NA), “min” (value &lt; noData are converted to NA), “max” (value &gt; noData are converted to NA). (DEFAULT=“exact”, only exact matches are converted to NA)
</p>
</td>
</tr>
<tr valign="top">
<td>
<code>valScale</code>
</td>
<td>
<p>
The scale factor to apply to the image values per the MODIS product. The adjustment is applied as VALUE * valScale + valAdd. For example, for the MOD15A2 LAI product, the scale factor is 0.1. (DEFAULT = 1)
</p>
</td>
</tr>
<tr valign="top">
<td>
<code>valAdd</code>
</td>
<td>
<p>
The addition value to apply to the image values per the MODIS product. The adjustment is applied as VALUE * valScale + valAdd. (DEFAULT = 0)
</p>
</td>
</tr>
<tr valign="top">
<td>
<code>outFile</code>
</td>
<td>
<p>
OPTIONAL name for an output NetCDF file. A NetCDF file will only be created if this file name is provided. Images will be exported after the “no data” and scale adjustments are made. If you want to do smoothing or other time series processing, do not export a NetCDF file here but do processing on the raster stack and then use ConvertStack2NC to export the processed brick to a NetCDF file.
</p>
</td>
</tr>
<tr valign="top">
<td>
<code>varName</code>
</td>
<td>
<p>
Name for the NetCDF export variable. Only required if outFile is provided.
</p>
</td>
</tr>
<tr valign="top">
<td>
<code>varUnit</code>
</td>
<td>
<p>
Units for the NetCDF export variable. Only required if outFile is provided.
</p>
</td>
</tr>
<tr valign="top">
<td>
<code>varLong</code>
</td>
<td>
<p>
Long name for the NetCDF export variable. Only required if outFile is provided.
</p>
</td>
</tr>
<tr valign="top">
<td>
<code>varNA</code>
</td>
<td>
<p>
Value to set for “NA” or “no data”. Default is -1.e+36. Only required if outFile is provided.
</p>
</td>
</tr>
<tr valign="top">
<td>
<code>pos1</code>
</td>
<td>
<p>
From MODIS orgTime: Start position of date in the filename (DEFAULT=10).
</p>
</td>
</tr>
<tr valign="top">
<td>
<code>pos2</code>
</td>
<td>
<p>
From MODIS orgTime: End position of date in the filename (DEFAULT=16).
</p>
</td>
</tr>
<tr valign="top">
<td>
<code>format</code>
</td>
<td>
<p>
From MODIS orgTime: How is the date formatted in the file. Default is “%Y%j” (‘YYYYDDD’). Read ?as.Date for for more information.
</p>
</td>
</tr>
</table>
<h3>
Details
</h3>
<p>
<code>ConvertRS2Stack</code> scans the specified directory and imports pre-processed MODIS TIF files matching the specified expression and combines them into an R raster stack object and, optionally, an output NetCDF file. Files in the input directory should be already processed through the <code>GetMODIS</code> tool or follow the same file naming convention used by the MODIS <code>runGdal</code> tool. See the R <a href="http://r-forge.r-project.org/projects/modis/">MODIS</a> package for more on those specifications.
</p>
<h3>
Value
</h3>
<p>
A raster stack.
</p>
<h3>
See Also
</h3>
<p>
Other MODIS: <code>CalcStatsRS</code>, <code>ConvertStack2NC</code>, <code>GapFillRS</code>, <code>GetMODIS</code>, <code>InsertRS</code>, <code>SmoothStack</code>
</p>
<h3>
Examples
</h3>
<pre class="r">
## Import the already processed LAI TIF images into a raster stack. Use 
## the full time series of images.

## Not run: 
lai.b &lt;- ConvertRS2Stack(&quot;/d6/adugger/WRF_Hydro/RS/MODIS_ARC/PROCESSED/BCNED_LAI&quot;,
                         &quot;*Lai_1km.tif&quot;, noData=100, noDataQual=&quot;max&quot;, 
                         valScale=0.1, valAdd=0)

## Export a subset of the already processed LAI TIF images into an output netcdf file

lai.b &lt;- ConvertRS2Stack(&quot;/d6/adugger/WRF_Hydro/RS/MODIS_ARC/PROCESSED/BCNED_LAI&quot;, 
                         &quot;*Lai_1km.tif&quot;, begin=&quot;2011.06.01&quot;, end=&quot;2011.06.30&quot;, 
                         noData=100, noDataQual=&quot;max&quot;, valScale=0.1, valAdd=0, 
                         outFile=&quot;BCNED_LAI.nc&quot;, varName=&quot;LAI&quot;,
                         varUnit=&quot;(m^2)/(m^2)&quot;, varLong=&quot;Leaf area index&quot;)

## End(Not run)
</pre>
</div>
</div>
<p><br> We can export the processed raster stack to a NetCDF file for use outside of R.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ConvertStack2NC</span>(lai.b, <span class="dt">outFile=</span><span class="kw">paste0</span>(dataPath, <span class="st">'/DOMAIN/FOURMILE_LAI_SUM13.nc'</span>), 
                <span class="dt">varName=</span><span class="st">&quot;LAI&quot;</span>, <span class="dt">varUnit=</span><span class="st">&quot;(m^2)/(m^2)&quot;</span>, <span class="dt">varLong=</span><span class="st">&quot;Leaf area index&quot;</span>, <span class="dt">varNA=</span>-<span class="fl">1.e+36</span>)</code></pre>
<p>Let’s take a look at the raster stack we created. List the available layers (labelled by date).</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">names</span>(lai.b)</code></pre>
<pre><code>##  [1] &quot;DT2013.03.06&quot; &quot;DT2013.03.14&quot; &quot;DT2013.03.22&quot; &quot;DT2013.03.30&quot;
##  [5] &quot;DT2013.04.07&quot; &quot;DT2013.04.15&quot; &quot;DT2013.04.23&quot; &quot;DT2013.05.01&quot;
##  [9] &quot;DT2013.05.09&quot; &quot;DT2013.05.17&quot; &quot;DT2013.05.25&quot; &quot;DT2013.06.02&quot;
## [13] &quot;DT2013.06.10&quot; &quot;DT2013.06.18&quot; &quot;DT2013.06.26&quot; &quot;DT2013.07.04&quot;
## [17] &quot;DT2013.07.12&quot; &quot;DT2013.07.20&quot; &quot;DT2013.07.28&quot;</code></pre>
<p>Plot a few of the images in the LAI raster stack. We can choose which layer to plot by index or name.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">plot</span>(lai.b, <span class="dv">1</span>)</code></pre>
<p><img src="" title alt width="700" height="350" /></p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">plot</span>(lai.b, <span class="st">&quot;DT2013.07.12&quot;</span>)</code></pre>
<p><img src="" title alt width="700" height="350" /></p>
</div>
<div id="apply-smoothing-filters" class="section level1">
<h1>Apply smoothing filters</h1>
<p>Apply a whittaker filter to smooth the LAI time series. This is generally not necessary for a derived product like LAI, but may be useful for products like NDVI, snow cover fraction, vegetated fraction, etc.</p>
<p>Try a highly smoothed filter.</p>
<pre class="sourceCode r"><code class="sourceCode r">lai.b.sm1 &lt;-<span class="st"> </span><span class="kw">SmoothStack</span>(lai.b, 
                         <span class="dt">outDirPath=</span><span class="kw">paste0</span>(<span class="kw">options</span>(<span class="st">&quot;MODIS_outDirPath&quot;</span>), <span class="st">'/FOURMILE_LAI_SMOOTH1'</span>), 
                         <span class="dt">outputAs=</span><span class="st">&quot;one&quot;</span>, <span class="dt">removeOutlier=</span><span class="ot">TRUE</span>, <span class="dt">outlierThreshold=</span><span class="fl">0.5</span>, 
                         <span class="dt">lambda=</span><span class="dv">2000</span>, <span class="dt">overwrite=</span><span class="ot">TRUE</span>)</code></pre>
<pre><code>## Loading required package: ptw</code></pre>
<pre><code>## Yearly 'lambda' is: 2000 
## Now changed with lambda*('length of input data period in days'/365) to: 783.5616 
## Data is in, start processing!</code></pre>
<p>Try a less aggressive filter.</p>
<pre class="sourceCode r"><code class="sourceCode r">lai.b.sm2 &lt;-<span class="st"> </span><span class="kw">SmoothStack</span>(lai.b, 
                         <span class="dt">outDirPath=</span><span class="kw">paste0</span>(<span class="kw">options</span>(<span class="st">&quot;MODIS_outDirPath&quot;</span>), <span class="st">'/FOURMILE_LAI_SMOOTH2'</span>), 
                         <span class="dt">outputAs=</span><span class="st">&quot;one&quot;</span>, <span class="dt">lambda=</span><span class="dv">100</span>, <span class="dt">overwrite=</span><span class="ot">TRUE</span>)</code></pre>
<pre><code>## Yearly 'lambda' is: 100 
## Now changed with lambda*('length of input data period in days'/365) to: 39.17808 
## Data is in, start processing!</code></pre>
<p>Calculate statistics and plot the domain means over time.</p>
<pre class="sourceCode r"><code class="sourceCode r">stats.lai.b &lt;-<span class="st"> </span><span class="kw">CalcStatsRS</span>(lai.b)
stats.lai.b.sm1 &lt;-<span class="st"> </span><span class="kw">CalcStatsRS</span>(lai.b.sm1)
stats.lai.b.sm2 &lt;-<span class="st"> </span><span class="kw">CalcStatsRS</span>(lai.b.sm2)</code></pre>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">head</span>(stats.lai.b)</code></pre>
<pre><code>## Warning: package 'pander' was built under R version 3.1.2</code></pre>
<table>
<colgroup>
<col width="9%"></col>
<col width="8%"></col>
<col width="8%"></col>
<col width="11%"></col>
<col width="13%"></col>
</colgroup>
<thead>
<tr class="header">
<th align="center">mean</th>
<th align="center">min</th>
<th align="center">max</th>
<th align="center">sd</th>
<th align="center">POSIXct</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="center">0.5207</td>
<td align="center">0.1</td>
<td align="center">1.3</td>
<td align="center">0.2497</td>
<td align="center">2013-03-06</td>
</tr>
<tr class="even">
<td align="center">0.5229</td>
<td align="center">0.1</td>
<td align="center">1.1</td>
<td align="center">0.2133</td>
<td align="center">2013-03-14</td>
</tr>
<tr class="odd">
<td align="center">0.4514</td>
<td align="center">0.1</td>
<td align="center">0.9</td>
<td align="center">0.1793</td>
<td align="center">2013-03-22</td>
</tr>
<tr class="even">
<td align="center">0.6486</td>
<td align="center">0.1</td>
<td align="center">1.6</td>
<td align="center">0.2217</td>
<td align="center">2013-03-30</td>
</tr>
<tr class="odd">
<td align="center">0.6793</td>
<td align="center">0.1</td>
<td align="center">1.3</td>
<td align="center">0.2613</td>
<td align="center">2013-04-07</td>
</tr>
<tr class="even">
<td align="center">0.2121</td>
<td align="center">0</td>
<td align="center">0.5</td>
<td align="center">0.09172</td>
<td align="center">2013-04-15</td>
</tr>
</tbody>
</table>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">with</span>(stats.lai.b, <span class="kw">plot</span>(POSIXct, mean, <span class="dt">typ=</span><span class="st">'l'</span>))
<span class="kw">with</span>(stats.lai.b.sm1, <span class="kw">lines</span>(POSIXct, mean, <span class="dt">col=</span><span class="st">'red'</span>))
<span class="kw">with</span>(stats.lai.b.sm2, <span class="kw">lines</span>(POSIXct, mean, <span class="dt">col=</span><span class="st">'blue'</span>))
<span class="kw">legend</span>(<span class="st">&quot;topleft&quot;</span>, <span class="kw">c</span>(<span class="st">&quot;MODIS Raw&quot;</span>, <span class="st">&quot;Smooth Filter 1&quot;</span>, <span class="st">&quot;Smooth Filter 2&quot;</span>), 
       <span class="dt">col=</span><span class="kw">c</span>(<span class="st">&quot;black&quot;</span>, <span class="st">&quot;red&quot;</span>, <span class="st">&quot;blue&quot;</span>), <span class="dt">lty=</span><span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>))</code></pre>
<p><img src="" title alt width="700" height="350" /></p>
</div>
<div id="export-to-forcing" class="section level1">
<h1>Export to forcing</h1>
<p>Export the second smoothed time series to the forcing data for use in future model runs. This requires a local NCO installation.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">InsertRS</span>(lai.b.sm2, <span class="dt">forcPath=</span><span class="kw">paste0</span>(dataPath, <span class="st">'/FORCING'</span>), <span class="dt">forcName=</span><span class="st">&quot;LDASIN_DOMAIN1&quot;</span>, 
         <span class="dt">varName=</span><span class="st">&quot;LAI&quot;</span>, <span class="dt">varUnit=</span><span class="st">&quot;(m^2)/(m^2)&quot;</span>, <span class="dt">varLong=</span><span class="st">&quot;Leaf area index&quot;</span>, <span class="dt">overwrite=</span><span class="ot">TRUE</span>)</code></pre>
<p><br><br></p>
</div>



<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>

</body>
</html>
